#include "stdafx.h"
#include "Configuration.h"
#include "../Core/Log.h"

Configuration::Configuration()
{
}

Configuration::~Configuration()
{
}

void Configuration::ParseFile(std::string p_FileName)
{
	Log::GetSingleton().Write(LogLevel_Info, "Loading configuration file ['" + p_FileName + "']");
	std::map<std::string, std::string>::clear();
	AdaptationData t_AD(p_FileName);
	std::multimap<std::string, std::string> t_Blank;
	std::vector<Dictionary> t_Result = t_AD.search(t_Blank);
	if(t_Result.size()==0) return;
	std::vector<Dictionary>::iterator p=t_Result.begin();
	for(Dictionary::iterator q=p->begin(); q!=p->end(); q++)
	{
		Log::GetSingleton().Write(LogLevel_Info, "Config ['" + q->first + "'] Value [" + q->second + "]");
		insert(std::pair<std::string, std::string>(q->first, q->second));
	}
	Log::GetSingleton().Write(LogLevel_Info, "Configuration file ['" + p_FileName + "'] loaded");
}

std::string Configuration::GetString(std::string p_Key)
{
	std::string t_RetVal;
	if(count(p_Key)>0)
	{
		t_RetVal = find(p_Key)->second;
	}
	else
	{
		Log::GetSingleton().Write(LogLevel_Error, "Configuration '" + p_Key + "' could not be found");
		throw std::exception();
	}
	return t_RetVal;
}

int Configuration::GetInt(std::string p_Key)
{
	return atoi(GetString(p_Key).c_str());
}

double Configuration::GetFloat(std::string p_Key)
{
	return atof(GetString(p_Key).c_str());
}
